﻿using RRQMMVVM;

namespace ViewModel.ViewModels
{
    public class ContactItemViewModel : ViewModelBase
    {
        public ContactItemViewModel()
        {
            this.InitiateSessionCommand = new ExecuteCommand(InitiateSession);
        }

        #region Command

        public ExecuteCommand InitiateSessionCommand { get; set; }

        #endregion Command

        #region 属性

        private string nickName;

        public string NickName
        {
            get { return nickName; }
            set { nickName = value; OnPropertyChanged(); }
        }

        private int account;

        /// <summary>
        /// 账号
        /// </summary>
        public int Account
        {
            get { return account; }
            set { account = value; OnPropertyChanged(); }
        }

        #endregion 属性

        private void InitiateSession()
        {
            foreach (var item in MainViewModel.Instance.WindowBody.RecentlyContactListItems)
            {
                if (item.Account == this.Account)
                {
                    MainViewModel.Instance.WindowBody.RecentlyListSelectItem = item;
                    MainViewModel.Instance.WindowBody.RecentlyButton();
                    MainViewModel.Instance.WindowBody.RecentlyContactText = string.Empty;
                    return;
                }
            }

            RecentlyContactItemViewModel recentlyContactItem = new RecentlyContactItemViewModel();
            recentlyContactItem.Account = this.Account;
            recentlyContactItem.NickName = this.NickName;
            recentlyContactItem.Parent = MainViewModel.Instance.WindowBody.RecentlyContactListItems;
            MainViewModel.Instance.WindowBody.RecentlyContactListItems.Add(recentlyContactItem);
            MainViewModel.Instance.WindowBody.RecentlyListSelectItem = recentlyContactItem;
            MainViewModel.Instance.WindowBody.RecentlyButton();
            MainViewModel.Instance.WindowBody.RecentlyContactText = string.Empty;
        }
    }
}